#include "mrc.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "AutoArchive.h"

#define PORT 18888
mrc::mrc(void)
{
}

mrc::~mrc(void)
{
}

int mrc::run(char *ip)
{
	int server = socket(AF_INET, SOCK_STREAM, 0);
	if (server < 1)
	{
		//perror("socket()");
		printf("socket() error");
		return -1;
	}

	struct sockaddr_in sin = {0};
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(server, (struct sockaddr*)&sin, sizeof(sin)) != 0)
	{
		//perror("bind()");
		printf("bind() error");
		return -1;
	}

	listen(server, 3);

	printf("server start up\n");
	char msg[] = "hello, i'm server";
	while (1)
	{
		struct sockaddr_in sinc = {0};
		int len = 0;
		int sc = accept(server, (struct sockaddr*)&sinc, (socklen_t*)&len);
		if (sc > 0)
		{
//			write(sc, msg, sizeof(msg));
			if (88 == talk(sc))
            {
                printf("88, server ended!\n");
                return 88;
            }
		}
	}


	return 0;
}

int mrc::talk(int sockfd)
{
	printf("============== new client connected ================\n");
	char buf[4097] = {0};
	while(1)
	{
        printf("waiting for client[%d] message!\n", sockfd);
		bzero(buf, sizeof(buf));
		int msglen = read(sockfd, buf, 4096);
        short type;
        CByteStream bs((unsigned char*)buf);
        bs>>type;
        printf("type: %d, len=%d\n", type, msglen);
        switch (type)
        {
            case 101: // register
                break;
            case 111:   // login
                {
                    bs.SetType((short)112);
                    write(sockfd, buf, msglen);
                    int len = 0;
                    bs>>len;
                    char *name = new char[len+1];
                    bzero(name, len+1);
                    bs.Read(name, len);
                    bs>>len;
                    char *pwd = new char[len+1];
                    bzero(pwd, len+1);
                    bs.Read(pwd, len);
                    printf("name: %s, pwd: %s\n", name, pwd);
                    delete[] name;
                    delete[] pwd;
                }
                break;
            case 151:
            case 161:
            case 181:
            case 0:
            case 201:   // logout
                close(sockfd);
                printf("client exit\n");
                return -1;
                break;
            case 211:
                {
                    close(sockfd);
                    printf("close client and exit the server!\n");
                    return 88;
                }
                break;
            default:
                printf("return msg back, %d\n", msglen);
                write(sockfd, buf, msglen);
                break;
        }
	}


	printf("=============== client disconnected ================\n");
	return 0;
}

